﻿@using SimpleIdServer.IdServer.Website.Stores.AcrsStore;
@using SimpleIdServer.IdServer.Website.Stores.AuthMethodsStore;
@using SimpleIdServer.IdServer.Website.Stores.ConfigurationDefinitionStore;
@using SimpleIdServer.IdServer.Website.Stores.LanguageStore;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@using System.Globalization;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.RegistrationWorkflowStore;
@inherits Fluxor.Blazor.Web.Components.FluxorLayout
@inject IState<RealmsState> state
@inject IState<LanguagesState> languagesState
@inject ContextMenuService contextMenuService
@inject IDispatcher dispatcher
@inject DialogService dialogService
@inject NavigationManager Navigation

<PageTitle>@Global.SidAdminUi</PageTitle>

<RadzenDialog />
<RadzenNotification />
<RadzenContextMenu />
<RadzenTooltip />

<RadzenLayout class="rz-shadow-3">
    <RadzenHeader>
        <div class="d-flex align-items-center">
            <div class="p-2">
                <RadzenSidebarToggle Click="@(() => sidebarExpanded = !sidebarExpanded)" />
            </div>
            <div class="p-2">
                <RadzenImage Path="_content/SimpleIdServer.IdServer.Website/images/SIDLogo.svg" Style="width: 40px" />
            </div>
            <div class="p-2">
                <RadzenText Style="margin: 0; color: white;" Text="SimpleIdServer" TextStyle="TextStyle.DisplayH6" />
            </div>
            <div class="ml-auto"></div>
            <div class="p-2">
                @if(!languagesState.Value.IsLoading)
                {
                    <RadzenDropDown Name="UserPropertyName" Class="w-100"
                                    Data=@languagesState.Value.Languages
                                    TValue="string"
                                    @bind-Value=@CurrentLanguage
                                    TextProperty="Description" ValueProperty="Code" />
                }
            </div>
            <div class="p-2">
                <RadzenLink Text="@Global.SignOut" Icon="logout" Path="/logout" />
            </div>
        </div>
    </RadzenHeader>
    @if(!isLoading)
    {
        <RadzenSidebar @bind-Expanded="@sidebarExpanded">
            <RadzenPanelMenu>
                <RadzenPanelMenuItem Text=@(string.Format(Global.ActiveRealm, activeRealm)) Path="/realm" />
                <RadzenPanelMenuItem Text="@Global.Manage" />
                <RadzenPanelMenuItem Text="@Global.Clients" Icon="apps" Path="/clients" />
                <RadzenPanelMenuItem Text="@Global.Scopes" Icon="visibility" Path="/scopes" />
                <RadzenPanelMenuItem Text="@Global.UserMgt">
                    <RadzenPanelMenuItem Text="@Global.Users" Icon="person" Path="/users" />
                    <RadzenPanelMenuItem Text="@Global.Groups" Icon="group" Path="/groups" />
                </RadzenPanelMenuItem>
                <RadzenPanelMenuItem Text="@Global.Authentication">
                    <RadzenPanelMenuItem Text="@Global.AuthenticationContext" Icon="security" Path="/acrs" />
                    <RadzenPanelMenuItem Text="@Global.Authentication" Icon="person" Path="/authentications/authmethods" />
                </RadzenPanelMenuItem>
                <RadzenPanelMenuItem Text="@Global.IdentityProvisioning">
                    <RadzenPanelMenuItem Text="@Global.ManualIdentityProvisioning" Icon="back_hand" Path="/registrationworkflows"></RadzenPanelMenuItem>
                    <RadzenPanelMenuItem Text="@Global.AutomaticIdentityProvisioning" Icon="refresh" Path="/identityprovisioning" />
                </RadzenPanelMenuItem>
                <RadzenPanelMenuItem Text="@Global.Configure" />
                <RadzenPanelMenuItem Text="@Global.CertificateAuthoritiesTitle" Icon="shield" Path="/certificateauthorities" />
                <RadzenPanelMenuItem Text="@Global.Auditing" Icon="source" Path="/auditing" />
            </RadzenPanelMenu>
        </RadzenSidebar>
        <RadzenBody>
            <div class="p-2">
                @Body
            </div>
        </RadzenBody>
    }
    else
    {
        <h3>@Global.Loading</h3>
    }
</RadzenLayout>

@code {
    bool sidebarExpanded = true;
    bool isLoading = true;
    string activeRealm;

    public string CurrentLanguage
    {
        get => CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
        set
        {
            if (CultureInfo.CurrentCulture.TwoLetterISOLanguageName != value)
            {
                var uri = new Uri(Navigation.Uri)
                    .GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
                var cultureEscaped = Uri.EscapeDataString(value);
                var uriEscaped = Uri.EscapeDataString(uri);

                Navigation.NavigateTo(
                    $"Culture/Set?culture={cultureEscaped}&redirectUri={uriEscaped}",
                    forceLoad: true);
            }
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            SubscribeToAction<GetAllConfigurationDefinitionsSuccessAction>((act) =>
            {
                isLoading = false;
                StateHasChanged();
            });
            SubscribeToAction<GetActiveSuccessRealmAction>((act) =>
            {
                activeRealm = act.Realm;
                dispatcher.Dispatch(new GetAllConfigurationDefinitionsAction());
                StateHasChanged();
            });
            SubscribeToAction<SelectRealmSuccessAction>((act) =>
            {
                activeRealm = act.Realm;
                StateHasChanged();
            });
        }
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        this.isLoading = true;
        dispatcher.Dispatch(new GetActiveRealmAction());
        dispatcher.Dispatch(new GetAllRealmAction());
        dispatcher.Dispatch(new GetAllAuthMethodAction());
        dispatcher.Dispatch(new GetLanguagesAction());
        dispatcher.Dispatch(new GetAllRegistrationWorkflowsAction());
        dispatcher.Dispatch(new GetAllAcrsAction());
    }
}